<?php

/** * Processing Class *
 * @package TIAP 
 * @subpackage Controller 
 * @category user 
 * @author Ibnu Triyono 
 * @description Handling updating info on advertiser's panel. changing all information on advertising,
 *     and payment for advertising
 * @link http://twitter.com/ibnutri 
 */

class Directory_panel extends Controller
{
    var $user_session_id, $session_user_id;
    var $countryList;
    function Directory_panel()
    {
        parent::Controller();
        //$this->output->enable_profiler(TRUE);
        $this->load->helper(array('form', 'tiap_helper', 'html'));
        $this->load->library('form_validation');
        $this->load->model(array('Directory_user_model', 'Settings_model','Directory_services_model',
            'Directory_company_model', 'Directory_photo_model','Directory_invoice_model'));

        if ($this->Directory_user_model->check_login())
        {
            $this->session_user_id = $this->session->userdata('directory_user_id');
        } 
        else
        {
            $this->session_user_id = "";
            redirect('directory/advertise');
            exit;
        }
        //$this->countryList = $this->db->get('country');
        $this->region_table='region';
        
    }
    function index()
    {
        $this->login();
    }
    function login()
    {
        $data['region_list']    = $this->_get_region_by_country(254);
		 
		$data['services_list'] = $this->Directory_services_model->get_list_services("services");
        
        $data['no_breadcrumb'] = 1;
        $submit_button = $this->input->post('submit');
        if ($submit_button != "")
        {
            $email = $this->input->post('email');
            $password = $this->input->post('password');
            $re_password = $this->input->post('re_password');

            $this->form_validation->set_rules('email', 'e-mail','required|valid_email|callback_check_email');

            if ($password != "" || $re_password != "")
            {
                $this->form_validation->set_rules('password', 'Password',
                    'required|min_length[6]');
                $this->form_validation->set_rules('re_password', 're-typed Password',
                    'required|min_length[6]|matches[password]');
            }
            if ($this->form_validation->run() == true)
            {
                $this->Directory_user_model->update_login_info($this->session_user_id, $email, $password);
                $data['notification'] = "Login information updated";
            }

        }
        $user_detail = $this->Directory_user_model->get_detail($this->session_user_id);
        $company_detail = $this->Directory_company_model->get_company_by_id($this->
            session_user_id);
        if (strtotime($company_detail['paid_until']) < date('U'))
        {
            $this->Directory_company_model->set_company_activation($company_detail['id'],
                'inactive');
        }

        $data['email'] = $user_detail['email'];
        $data['real_pass'] = mask_password($user_detail['password_real'], "*", 3);
        $data['company_detail'] = $company_detail;
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_login_view', $data);
    }

    function company()
    {
        $data = array();
        
        $data['region_list']    = $this->_get_region_by_country(224);
		 
		$data['services_list'] = $this->Directory_services_model->get_list_services("services");
        
        $submit_button = $this->input->post('submit');
        if ($submit_button != "")
        {
            $this->form_validation->set_rules('name', 'Company name', 'required');
            $this->form_validation->set_rules('description', 'Company Description',
                'required');
            $this->form_validation->set_rules('contact_number', 'Telephone number',
                'required');
            $this->form_validation->set_rules('physical_street', 'Physical street address',
                'required');
            $this->form_validation->set_rules('physical_suburb', 'Physical suburb address',
                'required');
            $this->form_validation->set_rules('physical_city', 'Physical city address',
                'required');
                
            //$this->form_validation->set_rules('physical_zipcode', 'Zipcode','required');
            $this->form_validation->set_rules('province_id', 'Province', 'required');
            //$this->form_validation->set_rules('city_name', 'City','required');
            $this->form_validation->set_rules('contact_email', 'email address', 'valid_email');
            if ($this->form_validation->run() == true)
            {
                $company_name = $this->input->post('name');
                $company_desc = $this->input->post('description');
                if ($this->input->post('website') == 'http://')
                {
                    $company_website = '';
                } else
                {
                    $company_website = substr($this->input->post('website'), 7);
                }
                //$company_website = $this->input->post('website');
                $contact_number = $this->input->post('contact_number');
                $mobile = $this->input->post('mobile_number');
                $fax_number = $this->input->post('fax_number');
                $contact_person = $this->input->post('contact_person');
                //add province name and city name
                $province_id = $this->input->post('province_id');                
                $city_name = $this->input->post('city_name');
                // location data
                $physical_street = $this->input->post('physical_street');
                $physical_suburb = $this->input->post('physical_suburb');
                $physical_city = $this->input->post('physical_city');
                $physical_zipcode = $this->input->post('physical_zipcode');
                $postal_street = $this->input->post('postal_street');
                $postal_suburb = $this->input->post('postal_suburb');
                $postal_city = $this->input->post('postal_city');
                $postal_zipcode = $this->input->post('postal_zipcode');
                $latitude = $this->input->post('latitude');
                $longitude = $this->input->post('longitude');
                $country_name = $this->input->post('country_name');
                $contact_email = $this->input->post('contact_email');
                // -- company logo
                $company_logo = $this->input->post('upload');
                $result = $this->Directory_company_model->update_company($this->session_user_id,
                    ($company_name), $company_desc, $company_website, $contact_number, $mobile,
                    $fax_number, $contact_email, $company_logo, $province_id, $city_name);
                //echo $this->Directory_company_model->db->last_query();   
                $this->Directory_company_model->update_location($this->session_user_id, $physical_street,
                    $physical_suburb, $physical_city, $physical_zipcode, $postal_street, $postal_suburb,
                    $postal_city, $postal_zipcode, $latitude, $longitude, $country_name);
                //die($this->Directory_company_model->db->last_query());

                $data['messageBox'] = 'Your changes have been saved';

            } 
            else
            {
                $data['messageBox'] = 'Your changes failed to saved ' . validation_errors();
                
            }
        }

       
            $company_detail = $this->Directory_company_model->get_company_by_id($this->session_user_id);
                    
        $data['company_detail'] = $company_detail;
       // $data['country_list']   = $this->Location_model->get_country();
        //$data['region_list']    = $this->Location_model->get_region_by_country(224);
        $data['no_breadcrumb']  = 1;
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";        
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_company_view', $data);
    }

    function products()
    {
        $productType = $this->uri->segment(4);
        $productType = $productType == '' ? 'products' : $productType;
        $data = array();
        $company_detail = $this->Directory_company_model->get_company_by_id($this->
            session_user_id);
        $submit_button = $this->input->post('submit');
        if ($submit_button != "")
        {
            $services = $this->input->post('services');
            $this->Directory_company_model->update_services($this->session_user_id, $services);
            $data['notification'] = "Services information updated";
        }
        $data['productType'] = $productType;
        $data['company_detail'] = $company_detail;
        $data['no_breadcrumb'] = 1;
        //$data['products_list'] = $this->Directory_company_model->get_services('products');
        //$data['services_list'] = $this->Directory_company_model->get_services('services');
        //$data['brands_list'] = $this->Directory_company_model->get_services('brands');
        
        $data['services_list_print'] =  $this->Directory_company_model->get_services_print();
        
        $data['services_list_web'] =  $this->Directory_company_model->get_services_web();
        
        $company_services_list = $this->Directory_company_model->
            get_company_all_services($this->session_user_id);
        if (!empty($company_services_list))
        {
            $company_services = array();
            foreach ($company_services_list as $key => $value)
            {
                $company_services[] = $value['id_services'];
            }
            $data['company_services'] = $company_services;
        }
        
        $data['region_list']    = $this->_get_region_by_country(224);
		 
		$data['services_list'] = $this->Directory_services_model->get_list_services("services");
        
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_products', $data);
    }
    /*
    function branch()
    {
        $data = array();
        $company_detail = $this->Directory_company_model->get_company_by_id($this->
            session_user_id);
        $branch_list = $this->Directory_branch_model->get_branchs($this->
            session_user_id);
        $data['company_detail'] = $company_detail;
        $data['branch_list'] = $branch_list;
        $data['no_breadcrumb'] = 1;
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_branch', $data);
    }
    function branch_create()
    {
        $data = array();
        $submit_button = $this->input->post('submit');
        $data['postalCountry'] = '224';

        if ($submit_button != "")
        {
            $this->form_validation->set_rules('branch_name', 'Branch name', 'required');
            $this->form_validation->set_rules('telephone', 'Telephone number', 'required');
            $this->form_validation->set_rules('email', 'email', 'valid_email');
            $this->form_validation->set_rules('physical_street', 'Physical street address',
                'required');
            $this->form_validation->set_rules('physical_suburb', 'Physical suburb address',
                'required');
            $this->form_validation->set_rules('physical_city', 'Physical city address',
                'required');
            //$this->form_validation->set_rules('physical_zipcode', 'Zipcode','required');
            if ($this->form_validation->run() == true)
            {
                $name = $this->input->post('branch_name');
                $telephone = $this->input->post('telephone');
                $fax = $this->input->post('fax');
                $email = $this->input->post('email');
                $contact = $this->input->post('contact_person');
                $branch_id = $this->Directory_branch_model->add_branch($this->session_user_id, $name,
                    $telephone, $fax, $email, $contact);
                // location data
                $physical_street = $this->input->post('physical_street');
                $physical_suburb = $this->input->post('physical_suburb');
                $physical_city = $this->input->post('physical_city');
                $physical_zipcode = $this->input->post('physical_zipcode');
                $postal_street = $this->input->post('postal_street');
                $postal_suburb = $this->input->post('postal_suburb');
                $postal_city = $this->input->post('postal_city');
                $postal_zipcode = $this->input->post('postal_zipcode');
                $latitude = $this->input->post('latitude');
                $longitude = $this->input->post('longitude');

                $provinceName = $this->input->post('province_name');
                $cityName = $this->input->post('city_town_1');
                $postalCountry = $this->input->post('postal_country');

                $this->Directory_branch_model->addPostalCountry($postalCountry);
                $this->Directory_branch_model->addCityAndProvinceName($cityName, $provinceName);
                $this->Directory_branch_model->update_location($branch_id, $physical_street, $physical_suburb,
                    $physical_city, $physical_zipcode, $postal_street, $postal_suburb, $postal_city,
                    $postal_zipcode, $latitude, $longitude);
                redirect('directory/panel/branch');
            }

        }
        $company_detail = $this->Directory_company_model->get_company_by_id($this->
            session_user_id);
        $data['company_detail'] = $company_detail;
        $data['no_breadcrumb'] = 1;
        $data['countryList'] = $this->countryList;
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_branch_create',
            $data);
    }
    function branch_edit()
    {
        $branch_id = $this->uri->segment(3);
        $data = array();
        $submit_button = $this->input->post('submit');
        if ($submit_button != "")
        {
            $this->form_validation->set_rules('branch_name', 'Branch name', 'required');
            $this->form_validation->set_rules('telephone', 'Telephone number', 'required');
            $this->form_validation->set_rules('email', 'email', 'valid_email');
            $this->form_validation->set_rules('physical_street', 'Physical street address',
                'required');
            $this->form_validation->set_rules('physical_suburb', 'Physical suburb address',
                'required');
            $this->form_validation->set_rules('physical_city', 'Physical city address',
                'required');
            //$this->form_validation->set_rules('physical_zipcode', 'Zipcode','required');
            if ($this->form_validation->run() == true)
            {
                $name = $this->input->post('branch_name');
                $telephone = $this->input->post('telephone');
                $fax = $this->input->post('fax');
                $email = $this->input->post('email');
                $contact = $this->input->post('contact_person');
                $this->Directory_branch_model->update_branch($this->session_user_id, $branch_id,
                    $name, $telephone, $fax, $email, $contact);
                // location data
                $physical_street = $this->input->post('physical_street');
                $physical_suburb = $this->input->post('physical_suburb');
                $physical_city = $this->input->post('physical_city');
                $physical_zipcode = $this->input->post('physical_zipcode');
                $postal_street = $this->input->post('postal_street');
                $postal_suburb = $this->input->post('postal_suburb');
                $postal_city = $this->input->post('postal_city');
                $postal_zipcode = $this->input->post('postal_zipcode');
                $latitude = $this->input->post('latitude');
                $longitude = $this->input->post('longitude');
                $provinceName = $this->input->post('province_name');
                $cityName = $this->input->post('city_town_1');
                $postalCountry = $this->input->post('postal_country');

                $this->Directory_branch_model->addPostalCountry($postalCountry);
                $this->Directory_branch_model->addCityAndProvinceName($cityName, $provinceName);
                $this->Directory_branch_model->update_location($branch_id, $physical_street, $physical_suburb,
                    $physical_city, $physical_zipcode, $postal_street, $postal_suburb, $postal_city,
                    $postal_zipcode, $latitude, $longitude);
                redirect('directory/panel/branch');
            }
        }

        $company_detail = $this->Directory_company_model->get_company_by_id($this->
            session_user_id);
        $data['company_detail'] = $company_detail;
        $branch_detail = $this->Directory_branch_model->get_branch_detail($branch_id);
        $branch_detail['postal_country_id'] = $branch_detail['postal_country_id'] == '' ?
            '224' : $branch_detail['postal_country_id'];

        $data['branch_detail'] = $branch_detail;
        $data['no_breadcrumb'] = 1;
        $data['countryList'] = $this->countryList;
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_branch_edit', $data);
    }
    function branch_delete()
    {
        $branch_id = $this->uri->segment(3);
        $this->Directory_branch_model->delete_branch($branch_id, $this->session_user_id);
        redirect('directory/panel/branch');
    }
    */
    function photo()
    {
        $data = array();
        $data['region_list']    = $this->_get_region_by_country(224);
		 
		$data['services_list'] = $this->Directory_services_model->get_list_services("services");
        
        $submit_button = $this->input->post('submit');
        if ($submit_button == "submit")
        {
            $file_name = $this->input->post('upload');
            $file_caption = $this->input->post('news_caption');
            $file_ori = $this->input->post('upload_ori');
            if ($file_name)
            {
                for ($i = 0; $i < count($file_name); $i++)
                {
                    $this->Directory_photo_model->add_photo($this->session_user_id, $file_name[$i],
                        $file_caption[$i], $file_ori[$i]);
                }
            }
        }
        if ($submit_button == "delete")
        {
            $photo_id = $this->input->post('photo_id');
            if ($photo_id)
            {
                for ($i = 0; $i < count($photo_id); $i++)
                {
                    $this->Directory_photo_model->delete_photo($photo_id[$i]);
                }
            }
        }
        $company_detail = $this->Directory_company_model->get_company_by_id($this->
            session_user_id);
        $data['company_photo'] = $this->Directory_photo_model->get_photo($this->
            session_user_id);
        $data['company_detail'] = $company_detail;
        $data['no_breadcrumb'] = 1;
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_photo_view', $data);
    }
    /*
    function news()
    {
        $data = array();
        $company_detail = $this->Directory_company_model->get_company_by_id($this->
            session_user_id);
        $news_list = $this->Directory_news_model->get_news($this->session_user_id);
        $data['company_detail'] = $company_detail;
        $data['news_list'] = $news_list;
        $data['no_breadcrumb'] = 1;
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_news', $data);
    }
    function news_create()
    {
        $data = array();
        $submit_button = $this->input->post('submit');
        if ($submit_button != "")
        {
            $this->form_validation->set_rules('heading', 'Heading', 'required');
            $this->form_validation->set_rules('story', 'Story', 'required');
            if ($this->form_validation->run() == true)
            {
                $heading = $this->input->post('heading');
                $story = $this->input->post('story');
                $news_id = $this->Directory_news_model->add_news($this->session_user_id, $heading,
                    $story);
                $file_name = $this->input->post('upload');
                $file_caption = $this->input->post('news_caption');
                if ($file_name)
                {
                    for ($i = 0; $i < count($file_name); $i++)
                    {
                        $this->Directory_news_model->insert_news_image($news_id, $file_name[$i], $i + 1,
                            $file_caption[$i]);
                    }
                }
                redirect('directory/panel/news');
            }
        }
        $company_detail = $this->Directory_company_model->get_company_by_id($this->
            session_user_id);
        $data['company_detail'] = $company_detail;
        $data['no_breadcrumb'] = 1;
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_news_create', $data);
    }
    function news_edit()
    {
        $data = array();
        $news_id = $this->uri->segment(3);
        $submit_button = $this->input->post('submit');
        if ($submit_button != "")
        {
            $this->form_validation->set_rules('heading', 'Heading', 'required');
            $this->form_validation->set_rules('story', 'Story', 'required');
            if ($this->form_validation->run() == true)
            {
                $heading = $this->input->post('heading');
                $story = $this->input->post('story');
                $file_name = $this->input->post('upload');
                $file_caption = $this->input->post('news_caption');
                if ($file_name)
                {
                    //for ($i = 0; $i < count($file_name); $i++) {
                    for ($i = 0; $i < 4; $i++)
                    {
                        $this->Directory_news_model->insert_news_image($news_id, $file_name[$i], $i + 1,
                            $file_caption[$i]);
                    }
                }
                $this->Directory_news_model->update_news($news_id, $heading, $story);
                redirect('directory/panel/news');
            }
        }
        $company_detail = $this->Directory_company_model->get_company_by_id($this->
            session_user_id);
        $data['company_detail'] = $company_detail;
        $news_detail = $this->Directory_news_model->get_news_detail($news_id);
        $data['news_detail'] = $news_detail;
        $data['no_breadcrumb'] = 1;
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_news_edit', $data);
    }
    function news_delete()
    {
        $news_id = $this->uri->segment(3);
        $this->Directory_news_model->delete_news($news_id, $this->session_user_id);
        redirect('directory/panel/news');
    }
    function delete_account()
    {
        $data = array();
        
        $data['region_list']    = $this->_get_region_by_country(224);
		 
		$data['services_list'] = $this->Directory_services_model->get_list_services("services");
        
        $submit_button = $this->input->post('submit');
        if ($submit_button != '')
        {
            $confirm_delete = $this->input->post('confirm_delete');
            if ($confirm_delete == 'delete')
            {
                $this->User_model->delete_user($this->session_user_id);
                //$this->User_model->logout_delete($this->session_user_id);
                redirect('directory/login/logout_delete');
            }
        }
        $this->load->view(TEMPLATE_NAME . '/front/profile/profile_delete', $data);
    }
    
    */
    
    function delete_advert()
    {
        $data = array();
        $data['region_list']    = $this->_get_region_by_country(224);
		 
		$data['services_list'] = $this->Directory_services_model->get_list_services("services");
        
        $submit_button = $this->input->post('submit');
        if ($submit_button != '')
        {
            if($this->input->post('delete_advertId') != '')
            {
                $this->Directory_company_model->delete_company($this->session_user_id); 
                
                //@session_destroy();
                
                redirect('directory/logout');
            }
        }
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_delete_view', $data);
    }
   
    function preview()
    {
        $data = array();
        $submit_button = $this->input->post('submit');
        $company_detail = $this->Directory_company_model->get_company_detail($this->
            session_user_id);
        $data['company_detail'] = $company_detail;
        $user_detail = $this->Directory_company_model->get_user($company_detail['id']);
        $data['logo_thumbnail_folder'] = $this->Directory_company_model->
            logo_folder_url . '/thumb/';
        $data['photos_thumbnail_folder'] = $this->Directory_company_model->
            photos_folder_url . '/thumb/';
        $data['photos_real_folder'] = $this->Directory_company_model->photos_folder_url .
            '/preview/';

        $data['selected_products_list'] = $this->Directory_company_model->
            get_company_services($company_detail['id'], 'products');
        $data['selected_services_list'] = $this->Directory_company_model->
            get_company_services($company_detail['id'], 'services');
        $data['selected_brands_list'] = $this->Directory_company_model->
            get_company_services($company_detail['id'], 'brands');
        $data['news_list'] = $this->Directory_company_model->get_company_news($company_detail['id']);
        $data['photos_list'] = $this->Directory_company_model->get_company_photos($company_detail['id']);
        $data['side_left_directory'] = 1;
        //$data['branchList'] = $this->Directory_branch_model->get_branchs($company_detail['id']);
        $data['no_breadcrumb'] = 1;
        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_preview', $data);

    }
    function payment()
    {
        
        //$this->config->set_item('ssl_page', 'true');
        //echo $this->config->item('ssl_page');

        //echo site_url();
       /* $this->load->model('Directory_invoice_model');
        $data = array();
        $company_detail = $this->Directory_company_model->get_company_by_id($this->session_user_id);
        if(empty($company_detail)){
            redirect(site_url('directory/panel/company'));
        }
*/
        $submit_button = $this->input->post('submit');
        $data['company_detail'] = $company_detail;
        $data['branch_list'] = $branch_list;
        $data['no_breadcrumb'] = 1;
        $data['listing_active'] = $this->Directory_company_model->get_listing_status($this->
            session_user_id);
        $data['payment_list'] = $this->Directory_invoice_model->get_payment_list($this->
            session_user_id);
        $data['subscription_type_list'] = $this->Directory_company_model->
            get_subscription_type();
        if ($submit_button != "")
        {
            $this->form_validation->set_rules('subscription_type', 'Subscription type','required');
            if($this->input->post('subscription_type')!=0){  ///added      
                $this->form_validation->set_rules('payment_type', 'Payment Type', 'required');
                }///added
            $subscription_type = $this->input->post('subscription_type');            
            $payment_type = $this->input->post('payment_type');
            $data['subscription_type'] = $subscription_type;
            $data['payment_type'] = $payment_type;
            if ($this->form_validation->run() == true)
            {
                $data['subscription_type'] = $this->Directory_company_model->
                    get_subscription_type($subscription_type);
                $data['subscription_selected'] = $subscription_type;
                switch ($payment_type)
                {
                    default:
                        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_payment', $data);
                        break;
                    case "bt":
                        $data['bank_list'] = $this->Directory_invoice_model->get_bank_info();
                        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_payment_bt', $data);
                        break;
                    case "cc":
                        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_payment_cc', $data);
                        break;
                }
            } else
            {
                $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_payment', $data);
            }
        } else
        {
           // $data["freeused"] = $this->Directory_invoice_model->check_invoice_free($this->session_user_id);//added
            $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_payment', $data);
        }

    }
    function payment_bt()
    {
        $this->check_ssl();
        $this->config->set_item('ssl_page', 'true');
                
        $this->load->model('Directory_invoice_model');
        $data = array();
        $data['no_breadcrumb'] = 1;
        $company_detail = $this->Directory_company_model->get_company_by_id($this->
            session_user_id);
        $data['company_detail'] = $company_detail;
        $submit_button = $this->input->post('submit');
        if ($submit_button != "")
        {
            $this->form_validation->set_rules('bank_select', 'Bank Selection', 'required');
            if ($this->form_validation->run() == true)
            {
                
                $bank_select = $this->input->post('bank_select');
                $subscription_selected = $this->input->post('subscription_selected');
                $data['bank_info'] = $this->Directory_invoice_model->get_bank_info($bank_select);
                $ref_code = $this->Directory_invoice_model->create_invoice_bt($this->
                    session_user_id, $subscription_selected, $bank_select);
                $data['ref_code'] = $ref_code;
                $this->load->view(TEMPLATE_NAME .
                    '/front/directory_guest/panel_payment_bt_result', $data);
            } else
            {
                $data['bank_list'] = $this->Directory_invoice_model->get_bank_info();
                $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_payment_bt', $data);
            }
        }
    }
    function payment_cc()
    {
        //$this->check_ssl();
        //$this->config->set_item('ssl_page', 'true');
        //	echo $this->config->item('ssl_page');

        //echo site_url();
        $this->load->model('Directory_invoice_model');
        $data = array();
        $data['no_breadcrumb'] = 1;
        $company_detail = $this->Directory_company_model->get_company_by_id($this->
            session_user_id);
        $data['company_detail'] = $company_detail;
        $submit_button = $this->input->post('submit');
        if ($submit_button != "")
        {
            $this->form_validation->set_rules('card_number', 'Card number', 'required');
            $this->form_validation->set_rules('card_holder', 'Card holder', 'required');
            $this->form_validation->set_rules('CVC', 'security number', 'required');
            $this->form_validation->set_rules('card_type', 'Card type', 'required');
            $this->form_validation->set_rules('txtConfirmation','Confirmation email','required|valid_email');
            
            $bank_select = $this->input->post('bank_select');
            $subscription_selected = $this->input->post('subscription_selected');
                        
            if ($this->form_validation->run() == true)
            {
                $card_number = $this->input->post('card_number');
                $card_holder = $this->input->post('card_holder');
                $CVC = $this->input->post('CVC');
                $expire_year = $this->input->post('expire_year');
                $expire_month = $this->input->post('expire_month');
                $card_type = $this->input->post('card_type');
                $confirmationEmail = $this->input->post('txtConfirmation');
                $budgetSelect = $this->input->post('payment_radio');
                if($budgetSelect=='1'){
                    $budgetPeriod = $this->input->post('budgetperiod');
                }
                else
                {
                    $budgetPeriod = '';
                }

                //$bank_select = $this->input->post('bank_select');
                //$subscription_selected = $this->input->post('subscription_selected');
                $return = $this->Directory_invoice_model->create_invoice_cc($this->
                    session_user_id, $subscription_selected);
                $ref_code = $return['ref_code'];
                $description = $return['description'];
                $total_paid = $return['total_paid'];
                $data['ref_code'] = $ref_code;
                $this->load->model('Mygate_model');
                $this->Mygate_model->set_card_info($card_number, $card_holder, $CVC, $expire_year,
                    $expire_month, $card_type,$budgetSelect,$budgetPeriod);
                $this->Mygate_model->set_item_info($ref_code, $description, $total_paid);
                $result_mygate = $this->Mygate_model->purchase();
                $this->session->unset_userdata('cdn');
                $this->session->unset_userdata('cdh');
                $this->session->unset_userdata('cdt');
                $this->session->unset_userdata('cvc');
                $this->session->unset_userdata('exm');
                $this->session->unset_userdata('exy');
                //print_r($result_mygate);
                if (!empty($result_mygate))
                {
                    //print_r($result_mygate);
                    if ($result_mygate['acs_url'] != '')
                    {
                        $this->session->set_userdata('cdn', $card_number);
                        $this->session->set_userdata('cdh', $card_holder);
                        $this->session->set_userdata('cdt', $card_type);
                        $this->session->set_userdata('cvc', $CVC);
                        $this->session->set_userdata('exm', $expire_month);
                        $this->session->set_userdata('exy', $expire_year);                        
                        $data['result_mygate'] = $result_mygate;
                        $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_payment_cc_3d',
                            $data);
                    } else
                    {
                        //print_r($result_mygate);
                        list($something, $mygate_result) = explode('||', $result_mygate[0]);
                        //echo "{$something} : ".$mygate_result;
                        $payment_success = ($mygate_result == '0' || $mygate_result == '1') ? 1 : "";
                        if ($payment_success != '')
                        {
                            $this->Directory_invoice_model->set_user_listing($this->session_user_id, '1');
                            $this->Directory_invoice_model->set_invoice_status($ref_code, 'paid');
                            $this->load->view(TEMPLATE_NAME .
                                '/front/directory_guest/panel_payment_cc_success', $data);
                        } else
                        {
                            $card_error = $result_mygate[2];
                            $card_error = explode('||', $card_error);
                            $data['card_error'] = $card_error[3];
                            $data['card_error_description'] = $card_error[4];
                            $this->load->view(TEMPLATE_NAME .
                                '/front/directory_guest/panel_payment_cc_failed', $data);
                        }
                    }
                } else
                {
                    redirect('directory/panel/payment');
                }
            } else
            {
                $data['bank_list'] = $this->Directory_invoice_model->get_bank_info();
                
                $data['subscription_selected'] = $subscription_selected;
                $data['subscription_type'] = $this->Directory_company_model->
                    get_subscription_type($subscription_selected);
                $data['subscription_selected'] = $subscription_selected;
                
                $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_payment_cc', $data);
            }
        } else
        {
            $data['bank_list'] = $this->Directory_invoice_model->get_bank_info();            
            $this->load->view(TEMPLATE_NAME . '/front/directory_guest/panel_payment_cc', $data);
        }
    }
    function payment_cc_2()
    {
        $pares = $this->input->post('PaRes');
        $transaction_id = $this->input->post('MD');
        $invoice_number = $this->uri->segment(3);
        if ($pares != '' && $transaction_id != '')
        {
            $company_detail = $this->Directory_company_model->get_company_by_id($this->
                session_user_id);
            $data['company_detail'] = $company_detail;
            $this->load->model('Mygate_model');
            $this->load->model('Directory_invoice_model');
            $invoice_detail = $this->Directory_invoice_model->get_invoice($invoice_number);
            $card_number = $this->session->userdata('cdn');
            $card_holder = $this->session->userdata('cdh');
            $card_type = $this->session->userdata('cdt');
            $CVC = $this->session->userdata('cvc');
            $expire_month = $this->session->userdata('exm');
            $expire_year = $this->session->userdata('exy');
            $this->Mygate_model->set_card_info($card_number, $card_holder, $CVC, $expire_year,
                $expire_month, $card_type);
            $this->Mygate_model->set_item_info($invoice_number, $invoice_detail['description'],
                $invoice_detail['total_paid']);
            $result_mygate = $this->Mygate_model->cmpi_authenticate($transaction_id, $pares);
            //print_r($result_mygate);
            $this->session->unset_userdata('cdn');
            $this->session->unset_userdata('cdh');
            $this->session->unset_userdata('cvc');
            $this->session->unset_userdata('exm');
            $this->session->unset_userdata('exy');
            $payment_success = ($mygate_result == '0') ? 1 : "";
            if ($payment_success != '')
            {
                $this->Directory_invoice_model->set_user_listing($this->session_user_id, '1');
                $this->Directory_invoice_model->set_invoice_status($invoice_number, 'paid');
                $this->load->view(TEMPLATE_NAME .
                    '/front/directory_guest/panel_payment_cc_success', $data);
            } else
            {
                $card_error = $result_mygate[2];
                $card_error = explode('||', $card_error);
                $data['card_error'] = $card_error[3];
                $data['card_error_description'] = $card_error[4];
                $this->load->view(TEMPLATE_NAME .
                    '/front/directory_guest/panel_payment_cc_failed', $data);
            }
        } else
        {
            redirect('directory/panel/payment');
        }
    }
   
    function check_email($val)
    {
        if (!$this->Directory_user_model->check_available_email($val, $this->
            session_user_id))
        {
            $this->form_validation->set_message('check_email', '%s ' . $val .
                ' is already on our system, please use another %s');
            return false;
        } else
        {
            return true;
        }
    }
    function check_ssl()
    {
        if ($_SERVER['HTTPS'] != "on")
        {
            $url = "https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
            header("Location: $url");
            exit;
        }
    }
    
    function _get_region($region_id){
    $this->db->where('id', $region_id);
    $query = $this->db->get($this->region_table);
    if($query->num_rows() > 0){
      return $query->row_array();
    }else{
      return FALSE;
    }

    return $query->result_array();
  }
  function _get_region_by_country($country_id){
    //$this->db->where('country', '254');
    $this->db->where('adm1code =', 'kyp');
     $this->db->order_by('name');
    $query = $this->db->get('region');
    return $query->result_array();
  }
  
}
/* End of file profile.php */
/* Location: ./system/application/controllers/directory_panel.php */
